#ifndef _MUNIX_STACK_H_
#define _MUNIX_STACK_H_

#define DEFAULT_STACK_SZ	16

template<class T>
struct StackNode
{
	T *	data;
	StackNode *	prev;
	StackNode *	next;
};

template<class T>
class Stack
{
public:
	Stack();
	Stack(uint8);
	~Stack() { delete [] stack; }

	void	Empty();
	uint8	Push(T *);
	T *		Pop();

protected:
	StackNode ** stack;
	uint8		sz,ct;
	uint8		pos;
};

template<class T>
T* Stack<class T>::Pop()
{
	--ct;
	return stack[--pos];
}

template<class T>
uint8 Stack<class T>::Push(T * d)
{
	stack[pos++]->data = d;
	++ct;
	return pos-1;
}

template<class T>
void Stack<class T>::Empty()
{
	StackNode * curr = stack;
	while(curr) {
		curr->data = (T*)NULL;
		curr = curr->next;	
	}
	ct = 0;
	pos = 0;
}	

template<class T>
Stack<class T>::Stack(uint8 s)
{
	stack = new StackNode * [s];
	sz = s;
	ct = 0;
	pos = 0;
	for(uint8 i = 0; i < sz; i++)
	{
		if (i == sz - 1)
			stack[i]->next = stack[0];
		else	
			stack[i]->next = stack[i+1];
		if (i == 0)
			stack[i]->prev = stack[sz-1];
		else
			stack[i]->prev = stack[i-1];
	}
}
	
template<class T>
Stack<class T>::Stack()
{
	stack = new StackNode * [DEFAULT_STACK_SZ];
	sz = DEFAULT_STACK_SZ;
	ct = 0;
	pos = 0;
	for(uint8 i = 0; i < sz; i++)
	{
		if (i == sz - 1)
			stack[i]->next = stack[0];
		else	
			stack[i]->next = stack[i+1];
		if (i == 0)
			stack[i]->prev = stack[sz-1];
		else
			stack[i]->prev = stack[i-1];
	}
}
	
		

#endif
